//
//  BannerView.m
//  SocketDemo
//
//  Created by apple on 17/2/23.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "BannerView.h"

@interface BannerView ()<UIScrollViewDelegate>
{
    CGRect _mainFrame;
    NSInteger _currentIndex;
}

@property (nonatomic , strong) UIScrollView * mainScrollView;
@property (nonatomic , strong) NSMutableArray<UIImageView *> * imageViewArray;
@end

@implementation BannerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _mainFrame = frame;
        self.imageViewArray = [NSMutableArray array];
        [self initUserInterface];
    }
    return self;
}

-(void)initUserInterface{
    [self addSubview:self.mainScrollView];
}


-(void)initImageContainer{
    [self.imageViewArray removeAllObjects];
    for (int i = 0; i < 3; i ++) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_mainFrame.size.width * i, 0, _mainFrame.size.width, _mainFrame.size.height)];
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleToFill;
        if (i == 1) {
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(option_touchImageView)];
            [imageView addGestureRecognizer:gesture];
        }
        [self.mainScrollView addSubview:imageView];
        [self.imageViewArray addObject:imageView];
    }
    _currentIndex = self.theIndex - 1;
    if (_currentIndex < 0) {
        _currentIndex = self.imageNameArray.count - 1;
    }
    [self dynamicLoadImage];
}


-(void)option_touchImageView{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickImageView)]) {
        [self.delegate didClickImageView];
    }
}



-(void)ybSuspendTimer{
    if (self.timer && self.isAutoScroll) {
        [self.timer setFireDate:[NSDate distantFuture]];
    }
}

-(void)ybStartTimer{
    if (self.timer && self.isAutoScroll) {
        [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:self.interval]];
    }
}

-(void)dynamicLoadImage{
    NSInteger index = _currentIndex;
    for (int i = 0; i < 3; i ++) {
        if (index >( self.imageNameArray.count - 1 )) {
            index = 0;
        }
        if (i == 1) {
            self.theIndex = index;
        }
        __block UIImageView * imageView = self.imageViewArray[i];
        NSString * string = self.imageNameArray[index];
        if ([string hasPrefix:@"http"]) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageNameArray[index]] placeholderImage:PlaceholderBIGImage];
        }
        else{
            imageView.image = [UIImage imageNamed:string];
        }
        index ++;
    }
    self.mainScrollView.contentOffset = CGPointMake(_mainFrame.size.width, 0);
    if (self.isShowPageControl && self.pageControl) {
        self.pageControl.currentPage = self.theIndex;
    }
    if (self.block) {
        self.block(self.theIndex);
    }
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.imageNameArray.count > 2) {
        if (self.mainScrollView.contentOffset.x >= 2 * _mainFrame.size.width) {
            [self leftScroll];
        }
        if (self.mainScrollView.contentOffset.x <= 0 ) {
            [self rightScroll];
        }
    }
    else{
        if (self.mainScrollView.contentOffset.x >= _mainFrame.size.width){
            if (self.isShowPageControl && self.pageControl) {
                self.pageControl.currentPage = 1;
            }
            if (self.block) {
                self.block(1);
            }
        }
        else{
            if (self.isShowPageControl && self.pageControl) {
                self.pageControl.currentPage = 0;
            }
            if (self.block) {
                self.block(0);
            }
        }
    }
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self ybSuspendTimer];
}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self ybStartTimer];
}

- (void)leftScroll
{
    _currentIndex ++;
    if (_currentIndex > self.imageNameArray.count - 1) {
        _currentIndex = 0;
    }
    [self dynamicLoadImage];
}

- (void)rightScroll
{
    _currentIndex --;
    if (_currentIndex < 0) {
        _currentIndex = self.imageNameArray.count - 1;
    }
    [self dynamicLoadImage];
}


-(UIScrollView *)mainScrollView
{
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _mainFrame.size.width, _mainFrame.size.height)];
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.bounces = NO;
        _mainScrollView.contentSize = CGSizeMake(_mainFrame.size.width * 3, _mainFrame.size.height);
        _mainScrollView.delegate = self;
    }
    return _mainScrollView;
}


-(void)setImageNameArray:(NSArray *)imageNameArray
{
    if ([_imageNameArray isEqualToArray:imageNameArray] && imageNameArray.count > 0) {
        return;
    }
    _imageNameArray = imageNameArray;
    if (_mainScrollView.subviews.count > 0) {
        for (UIView * view in _mainScrollView.subviews) {
            [view removeFromSuperview];
        }
    }
    
    [self cannelBannerScroll];
    
    if (imageNameArray.count == 0) {
        _mainScrollView.contentSize = CGSizeMake(_mainFrame.size.width, _mainFrame.size.height);
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _mainFrame.size.width, _mainFrame.size.height)];
        imageView.image = PlaceholderBIGImage;
        [_mainScrollView addSubview:imageView];
        if (self.isShowPageControl && self.pageControl) {
            self.pageControl.numberOfPages = 1;
            self.pageControl.currentPage = 0;
            [self addSubview:self.pageControl];
        }
    }
    else if (imageNameArray.count == 1) {
        _mainScrollView.contentSize = CGSizeMake(_mainFrame.size.width, _mainFrame.size.height);
        __block UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _mainFrame.size.width, _mainFrame.size.height)];
        imageView.contentMode = UIViewContentModeScaleToFill;
        NSString * string = imageNameArray[0];
        if ([string hasPrefix:@"http"]) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageNameArray[0]] placeholderImage:PlaceholderBIGImage];
        }else{
            imageView.image = [UIImage imageNamed:imageNameArray[0]];
        }
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(option_touchImageView)];
        [imageView addGestureRecognizer:gesture];
        [_mainScrollView addSubview:imageView];
        if (self.isShowPageControl && self.pageControl) {
            self.pageControl.numberOfPages = 1;
            self.pageControl.currentPage = 0;
            [self addSubview:self.pageControl];
        }
        if (self.block) {
            self.block(0);
        }
    }
    else if (imageNameArray.count == 2) {
        _mainScrollView.contentSize = CGSizeMake(_mainFrame.size.width * 2, _mainFrame.size.height);
        __block UIImageView * imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _mainFrame.size.width, _mainFrame.size.height)];
        imageView1.contentMode = UIViewContentModeScaleToFill;
        NSString * string = imageNameArray[0];
        if ([string hasPrefix:@"http"]) {
            [imageView1 sd_setImageWithURL:[NSURL URLWithString:imageNameArray[0]] placeholderImage:PlaceholderBIGImage];
        }
        else{
            imageView1.image = [UIImage imageNamed:imageNameArray[0]];
        }
        imageView1.userInteractionEnabled = YES;
        UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(option_touchImageView)];
        [imageView1 addGestureRecognizer:gesture];
        [_mainScrollView addSubview:imageView1];
        __block UIImageView * imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(_mainFrame.size.width, 0, _mainFrame.size.width, _mainFrame.size.height)];
        imageView2.contentMode = UIViewContentModeScaleToFill;
        NSString * string1 = imageNameArray[1];
        if ([string1 hasPrefix:@"http"]) {
            [imageView2 sd_setImageWithURL:[NSURL URLWithString:imageNameArray[1]] placeholderImage:PlaceholderBIGImage];
        }
        else{
            imageView2.image = [UIImage imageNamed:imageNameArray[1]];
        }
        imageView2.userInteractionEnabled = YES;
        UITapGestureRecognizer * gesture2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(option_touchImageView)];
        [imageView2 addGestureRecognizer:gesture2];
        [_mainScrollView addSubview:imageView2];
        if (self.isShowPageControl && self.pageControl) {
            self.pageControl.numberOfPages = 2;
            self.pageControl.currentPage = 0;
            [self addSubview:self.pageControl];
        }
        if (self.block) {
            self.block(0);
        }
    }
    else{
        _mainScrollView.contentSize = CGSizeMake(_mainFrame.size.width * 3, _mainFrame.size.height);
        if (self.isShowPageControl && self.pageControl) {
            self.pageControl.numberOfPages = imageNameArray.count;
            self.pageControl.currentPage = 0;
            [self addSubview:self.pageControl];
        }
        [self initImageContainer];
        if (self.isAutoScroll) {
            if (self.interval == 0) {
                self.interval = 10;
            }
            if (self.timer) {
                [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:self.interval]];
            }
            else{
                self.timer = [NSTimer timerWithTimeInterval:self.interval target:self selector:@selector(moreBannerAutoScroll) userInfo:nil repeats:YES];
                [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
            }
        }
    }
    
}


-(void)moreBannerAutoScroll{
    [self.mainScrollView setContentOffset:CGPointMake(2 * _mainFrame.size.width, 0) animated:YES];
}

-(void)cannelBannerScroll{
    if (self.timer) {
        [self.timer invalidate];
    }
}




@end
